const commentsModel=require('../model/comments')
module.exports={
    getCommentsPage(req,res){
        // if (req.session.isLogin) {
            res.render('admin/comments');
        // } else {
            // res.send('<script>location.href="http://127.0.0.1:8080/admin/user/login.html"</script>')
        // }
    },
    getAllComments(req,res){
        commentsModel.getAllComments(data=>{
            // console.log(data);
            let response = data instanceof Array ?{code:200,msg:'ok',data}:{code:500,msg:'获取失败'};
            res.send(response);
        })
    },
    getCommentsBypage(req,res){
        let {pageIndex, pageSize}=req.query;
        commentsModel.getCommentsBypage(pageIndex, pageSize,data=>{
            let response = data instanceof Array ?{code:200,msg:'ok',data}:{code:500,msg:'获取失败'};
            commentsModel.getCommentsCount(result=>{
                // console.log(result);
                // console.log(result.RowDataPacket.total);
                 result ? response.total = Math.ceil(result.total / pageSize) : '';
                res.send(response);
            })
        })
    },
    deleteCommentsById(req,res){
        let {id}=req.query;
        commentsModel.deleteCommentsById(id,result=>{
            console.log(result);
            res.send(result.affectedRows===1?{code:200,msg:'删除成功'}:{code:500,msg:'删除失败'});
        })
    }
}